<style>
.modal_historybox{padding:20px;}
.modal_history_top{ padding:10px 0; border-bottom:1px solid #CCC; margin-bottom:10px}
ul.mhlist{ background:#EEE; height:400px; overflow-y:scroll;}
ul.mhlist li{ padding:10px; cursor:pointer; border-bottom:1px solid #CCC;}
ul.mhlist li:hover,ul.mhlist li.isselect{ background:#009688; color:#FFF;}
ul.mhlist li h3{ font-size:20px; float:left; line-height:30px; width:100px; margin-right:5px;}
</style>
<div class="modal_history" style="display:none">
  <div class="modal_historybox">
    <div class="modal_history_top">
      <form action="" method="post" class="layui-form" onsubmit="return false;" enctype="multipart/form-data">
        <div class="secrchinput" style="margin-top:0">
          <button type="submit" onclick="funExtend_historyGetorderlist()"><i class="fa fa-search"></i></button>
          <input type="text" name="modal_history_txtTopupSeachStr" id="modal_history_txtTopupSeachStr" placeholder="输入单号">
        </div>
        <div class="layui-btn-group">
          <button class="layui-btn layui-btn-primary layui-btn-small" day="1">今天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="2">昨天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="3">最近3天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="4">最近7天</button>
          <button class="layui-btn layui-btn-primary layui-btn-small" day="5">本月</button>
        </div>
      </form>
    </div>
    <div class="row">
    	<div class="col-md-3">
        	<ul class="mhlist">
            	
            </ul>
        </div>
        <div class="col-md-9">
        	<div class="layui-form" style="margin-bottom:10px;">
              <table class="layui-table modal_historydetail">
                <tbody>
                    <tr>
                        <td width="30%"><b>人数</b> <span id="mmh_person">0</span> <input type="hidden" id="mmh_id" value="0"/></td>
                        <td width="30%"><b>茶位</b> <span id="mmh_teafee">0</span></td>
                        <td width="40%"><b>订单金额</b> <span id="mmh_orderfee">0</span></td>
                    </tr>
                    <tr>
                        <td><b>合计</b> <span id="mmh_total">0</span></td>
                        <td colspan="2"><b>状态</b> <span id="mmh_status">0</span></td>
                    </tr>
                </tbody>
              </table>
            </div>
            <div class="layui-form" style="height:290px; overflow-y:scroll">
              <table class="layui-table" lay-skin="line">
              	<thead>
                  <tr>
                    <th><input type="checkbox" lay-skin="primary" lay-filter="checkall"></th>
                    <th>序号</th>
                    <th>品名</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>小计</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody id="mhgoodslist">
                    
                </tbody>
              </table>
            </div>
        </div>
        
    </div>
    <div class="text-right"> <button class="layui-btn modal_disctprint">区域打印</button> <button class="layui-btn modal_historyboxprint1">整单打印</button> <button class="layui-btn modal_historyboxprint">前台打印</button> <button class="layui-btn modal_historyboxprintc">厨打</button> <button class="layui-btn" onclick="funExtend_refundreason(0,'')">整单退款</button> <button class="layui-btn layui-btn-primary modal_historyboxclosebtn">关闭</button></div>
  </div>
</div>


<div class="modal_printer" style="display:none">
    <div class="modal_printerbox" style="padding:10px;">
        <p><select id="printerselect" lay-verify="required" lay-ignore="" class="form-control">
            <option value="0" selected>选择打印机</option>
            {loop $printerlist $row}
            <option value="{$row['id']}">{$row['title']}</option>
            {/loop}
        </select></p>
    </div>
</div>

<style>
.modal_tuikuan{padding:20px;}
</style>
<div class="modal_tuikuan" style="display:none">
    <div class="modal_tuikuanybox">
        <div class="">
            <form action="" method="post" id="modal_tuikuanform" class="layui-form" onsubmit="return false;" enctype="multipart/form-data">
                <div class="layui-form-item">
                    <label class="layui-form-label">退款类型</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" readonly id="mtk_type" value="商品退款" />
                    </div>
                </div>
                <div class="layui-form-item mtk_good">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" readonly id="mtk_goodname" />
                        <input type="hidden" class="layui-input" id="mtk_id" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">退款原因</label>
                    <div class="layui-input-block">
                        <select id="mtk_reason" lay-ignore class="form-control">
                            {if !$shop['refundreason']}
                            <option value="客户原因">客户原因</option>
                            {else}
                            {php $refundreasonAry=explode('|',$shop['refundreason']);}
                            {loop $refundreasonAry $row}
                            <option value="{$row}">{$row}</option>
                            {/loop}
                            {/if}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item mtk_good">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-block">
                        <input type="number" min="1" price="" max="3" class="form-control" id="mtk_num"/>
                    </div>
                </div>
                <div class="layui-form-item mtk_good">
                    <label class="layui-form-label">商品回收</label>
                    <div class="layui-input-block">
                        <input type="radio" name="mtk_getback" value="0" title="不回收">
                        <input type="radio" name="mtk_getback" value="1" title="回收" checked>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">金额</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" readonly id="mtk_fee"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">付款方式</label>
                    <div class="layui-input-block">
                        <select id="mtk_paytype" lay-ignore class="form-control">
                            <option value="2">现金</option>
                            <option value="99">原路返回</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" id="mtk_remark" />
                    </div>
                </div>
                <div class="text-right">
                    <button type="button" class="layui-btn mtk_btnsubmit">确认退款</button>
                    <button type="button" class="layui-btn layui-btn-primary mtk_btnclose">关闭</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script language="javascript">
function funExtend_history(){
	layer.open({
		type: 1,
		title:0,
		area:"790px",
		skin: 'layui-layer-rim',
		content:$(".modal_history").show(),
		success: function(layero, index){
			layui.use(['form'], function(){
				var form = layui.form;
				form.render();
				form.on('checkbox(checkall)', function(data){
					$(".modal_historybox .layui-table input:checkbox").prop("checked",data.elem.checked);
					form.render("checkbox");
				});
				
			});
			$(".modal_historybox .layui-btn-group .layui-btn").bind("click",function(){
				$(this).removeClass("layui-btn-primary").siblings().addClass("layui-btn-primary");
				$(this).addClass("isselect").siblings().removeClass("isselect");
				funExtend_historyGetorderlist();
			});
			$(".modal_historybox .layui-btn-group .layui-btn").eq(0).click();
			$(".modal_historyboxclosebtn").bind("click",function(){
				layer.close(index);
			});
			$(".modal_historyboxprint").bind("click",function(){
				if($("#mhgoodslist .layui-form-checked").size()==0){
					layer.msg("请选择要打印的商品",{icon:2,time:1000});
					return false;
				}
				var idary=Array();
				$("#mhgoodslist .layui-form-checked").each(function(index, element) {
                    idary.push($(this).parent().parent().attr("mid"));
                });
				fun_print(2,$("#mmh_id").val(),idary.join(","));
			});
			$(".modal_historyboxprint1").bind("click",function(){
				if($("#mmh_id").val()=="0")return;
				fun_print(1,$("#mmh_id").val());
			});

            $(".modal_disctprint").bind("click",function(){
                if($("#mmh_id").val()=="0")return;
                funExtend_printer();
            });

			/*厨打*/
			$(".modal_historyboxprintc").bind("click",function(){
				if($("#mhgoodslist .layui-form-checked").size()==0){
					layer.msg("请选择要打印的商品",{icon:2,time:1000});
					return false;
				}
				var idary=Array();
				$("#mhgoodslist .layui-form-checked").each(function(index, element) {
                    idary.push($(this).parent().parent().attr("mid"));
                });
				fun_print(3,$("#mmh_id").val(),idary.join(","));
			});
		},
		end:function(){
			$(".modal_history").hide();
            $(".modal_historyboxprint1").unbind("click");
			$(".modal_historyboxclosebtn").unbind("click");
			$(".modal_historyboxprint").unbind("click");
			$(".modal_historyboxprintc").unbind("click");
            $(".modal_disctprint").unbind("click");
            $("#mhgoodslist").empty();
            $(".modal_historydetail td span").text("");
            $("#modal_history_txtTopupSeachStr").val("");
            $("#mmh_id").val("");
		}
	});
}
function funExtend_printer(){
    layer.open({
        type: 1,
        area:"400px",
        title:"区域打印",
        content:$(".modal_printer").show(),
        btn:['确认','取消'],
        btn1:function(indexs,layero){
            layer.close(indexs);
            var printer=$("#printerselect").val();
            var oid=$("#mmh_id").val();
            iskey="";
            var idary=[];
            $("#mhgoodslist .layui-form-checked").each(function(index, element) {
                idary.push($(this).parent().parent().attr("mid"));
                iskey=idary.join(",");
            });
            $("#print_iframe").removeAttr("src").attr("src","{$_W['siteroot']}app/index.php?i={$_W['uniacid']}&c=entry&ptype=10&m=j_hmoney&do=print&orderid="+oid+"&iskey="+iskey+"&printer="+printer+"&userid={php echo $userid}");
        },
        success: function(layero, index){
            $("#printerselect").val(0);
        },
        end:function(){
            $(".modal_printer").hide();
        }
    });
}
function funExtend_historyGetorderlist(){
	var datetype=$(".modal_historybox .layui-btn-group .isselect").size() ? $(".modal_historybox .layui-btn-group .isselect").attr("day"):0 ;
	var keyword=$("#modal_history_txtTopupSeachStr").val();
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'getorderhistroy'))}",{datetype:datetype,keyword:keyword},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		$(".mhlist").empty();
		
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return ;
		}
		var list=result.list;
		for(var i in list){
			var temp='<li oid="'+list[i].id+'" onclick="funExtend_historyselect('+list[i].id+')"><h3>'+list[i].title+'</h3><div><small>'+list[i].out_trade_no+'</small></div><div>'+fromatTime(list[i].createtime)+'</div></li>';
			$(".mhlist").append(temp);
		}
	});
}
function funExtend_historyselect(id){
	$(".mhlist li[oid='"+id+"']").addClass("isselect").siblings().removeClass("isselect");
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'gethistoryview'))}",{id:id},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		$("#mhgoodslist").empty();
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return ;
		}
		var trade=result.trade;
		$("#mmh_id").val(trade.id);
		$("#mmh_person").text(trade.person);
		$("#mmh_teafee").text(fee2num(trade.teafee));
		$("#mmh_orderfee").text(fee2num(trade.order_fee));
		//$("#mmh_total").text(fee2num(parseInt(trade.order_fee)+parseInt(trade.teafee)));
		$("#mmh_total").text(fee2num(parseInt(trade.total_fee)));
		var tmp='';
        if(parseInt(trade.status)==3){
            tmp='<span class="label label-danger">部分退款</span> ';
        }else if(parseInt(trade.status)==4){
            tmp='<span class="label label-danger">已退款</span> ';
        } else {
            if(parseInt(trade.status)==0){
                tmp+='<span class="label label-default">未点餐</span> ';
            }else if(parseInt(trade.status)==1){
                tmp+='<span class="label label-info">已点餐</span> ';
            }else if(parseInt(trade.status)==2){
                tmp+='<span class="label label-info">完成</span> ';
            }
            if(parseInt(trade.ispay)){
                tmp+='<span class="label label-success">已支付</span> <button class="layui-btn layui-btn-danger layui-btn-small" onclick="fun_flipOrder()">反结账</button>';
            }else{
                tmp+='<span class="label label-default">未付款</span> ';
                if(parseInt(trade.paid_fee1))tmp+='<button class="layui-btn layui-btn-danger layui-btn-small" onclick="fun_checkOrder('+trade.id+')">检查支付</button>';

            }
        }
		$("#mmh_status").html(tmp);
		var goods=result.goods;
		for(var i=0;i<goods.length;i++){
            var temp='<tr mid="'+goods[i].id+'"><td><input type="checkbox" name="mid['+goods[i].id+']" value="'+goods[i].id+'" lay-skin="primary"></td><td>'+(i+1)+'</td><td class="mtrgoodname">'+goods[i].title+'</td><td  class="mtrnum">'+goods[i].num+'</td><td class="mtrprice">'+goods[i].price+'</td><td class="mtrtotle">'+fee2num(goods[i].total_fee)+'</td><td>';
            temp+=parseInt(goods[i].isprint)  ? '<span><i class="fa fa-print"></i></span> ' :'';
            if(parseInt(goods[i].ispay) && parseInt(goods[i].num) && parseInt(goods[i].total_fee)){
                temp+='<a href="javascript:funExtend_refundreason(1,'+goods[i].id+')">退款</a>';
            }
            temp+='</td></tr>';
			$("#mhgoodslist").append(temp);
		}
		layui.use(['form'], function(){
			var form = layui.form;
			form.render();
		});
	});
}
function fun_flipOrder(){
    var orderid=$("#mmh_id").val();
    if($(".mhlist .isselect").size()==0 || parseInt(orderid)==0){
        layer.msg("请选选择订单",{icon:2,time:1800});
        return;
    }
    layer.confirm('是否要进行反结账操作，本操作仅限店长级别权限使用？', {btn:['确认', '关闭']},function(index1){
        layer.close(index1);
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        $.post("{php echo $this->createMobileUrl('index',array('op'=>'fliporder'))}",{orderid:orderid},function(returnstr) {
            layer.close(index2);
            console.log(returnstr);
            $("#mhgoodslist").empty();
            var result = eval("(" + returnstr + ")");
            if (result.success) {
                layer.msg("反结账成功，请马上处理", {icon: 1, time: 2000},function(){
                    funExtend_historyGetorderlist();
                });
            }else{
                layer.msg(result.msg, {icon: 2, time: 1000});
                return;
            }
        });
    });

}
function fun_checkOrder(orderid){
    var index2=layer.load(1, {shade: [0.1,'#fff']});
    $.post("{php echo $this->createMobileUrl('index',array('op'=>'paywaitpassword'))}",{orderid:orderid},function(returnstr) {
        layer.close(index2);
        console.log(returnstr);
        var result = eval("(" + returnstr + ")");
        if (result.success) {
            layer.msg("该订单支付成功", {icon: 1, time: 2000});
            if(result.isnew)fun_paysuccess(orderid);
        }else{
            layer.msg(result.msg, {icon: 2, time: 2000});
            return;
        }
    });
}
function funExtend_refundreason(type,id){
    var title=$(".modal_tuikuanreasonbox .isselect").text();
    if(type==1){
        funExtend_historyTuikuan(id,title);
    }else{
        funExtend_historyTuikuan(0);
    }
}
function funExtend_historyTuikuan(goodid){
    $("#modal_tuikuanform")[0].reset();
    var refundertype=parseInt(goodid) ? 1: 0;
    var formtitle=refundertype ? "商品退款":"订单退款";
    if(refundertype){
        $("#mtk_id").val(goodid);
        $(".mtk_good").show();
        $("#mtk_goodname").val($("#mhgoodslist tr[mid='"+goodid+"'] .mtrgoodname").text());
        $("#mtk_num").attr("max",$("#mhgoodslist tr[mid='"+goodid+"'] .mtrnum").text());
        $("#mtk_num").attr("price",$("#mhgoodslist tr[mid='"+goodid+"'] .mtrprice").text());
        $("#mtk_num").val("1");
        $("#mtk_fee").attr("readonly",true).val($("#mhgoodslist tr[mid='"+goodid+"'] .mtrprice").text());
    }else{
        $("#mtk_id").val("0");
        $(".mtk_good").hide();
        $("#mtk_goodname").val('');
        $("#mtk_fee").attr("readonly",true).val($("#mmh_total").text());
    }
    var remark=$("#mtk_reason").val();
    layer.open({
        type: 1,
        title:formtitle,
        area:"400px",
        skin: 'layui-layer-rim',
        content:$(".modal_tuikuan").show(),
        success: function(layero, index){
            $("#mtk_num").bind("change",function(){
                if(!refundertype)return;
                var tempval=parseInt($(this).val());
                var maxnum=parseInt($(this).attr("max"));
                var minnum=1;
                if(tempval>maxnum)$(this).val(maxnum);
                if(tempval<minnum)$(this).val(minnum);
                tempval=parseInt($(this).val());
                var price=parseInt(parseFloat($("#mtk_num").attr("price"))*100);
                var fee=fee2num(price*tempval);
                $("#mtk_fee").val(fee);
            });
            $(".mtk_btnclose").bind("click",function(){
                layer.close(index);
            });
            $(".mtk_btnsubmit").bind("click",function(){
                var orderid=$("#mmh_id").val();
                var reason=$("#mtk_reason").val();
                var paytype=$("#mtk_paytype").val();
                var num=parseInt($("#mtk_num").val());
                var fee=parseFloat($("#mtk_fee").val());
                var maxfee=parseFloat($("#mtk_fee").attr("fee"));
                var isgetback=$(".modal_tuikuan .layui-form-radioed").prev().val();
                if(fee>num*maxfee && goodid){
                    layer.msg("退款金额不能大于付款金额",{icon:2,time:1800});
                    return;
                }
                if(num<=0 && goodid){
                    layer.msg("已退款",{icon:2,time:1800});
                    return;
                }
                layer.confirm('是否确认退款？', {btn:['确认退款', '关闭']},function(){
                    var index3=layer.load(1, {shade: [0.1,'#fff']});
                    $.post("{php echo $this->createMobileUrl('index',array('op'=>'refundorder'))}",{id:goodid,num:num,fee:fee,remark:remark,orderid:orderid,reason:reason,paytype:paytype,isgetback:isgetback},function(returnstr2){
                        layer.close(index3);
                        console.log(returnstr2);
                        var result2=eval("("+returnstr2+")");
                        layer.close(index);
                        if(result2.success){
                            fun_print(5,result2.refundid);
                            layer.msg("退款成功",{icon:1,time:1500});
                            funExtend_historyselect(orderid);
                        }else{
                            layer.msg(result2.msg,{icon:2,time:1500});
                            return ;
                        }
                    });
                });
            });

        },
        end:function(){
            $(".modal_tuikuan").hide();
            $("#mtk_num").unbind("change");
            $(".mtk_btnclose").unbind("click");
            $(".mtk_btnsubmit").unbind("click");
        }
    });
}

</script>